<template>
	<view class="securitys">
		<view v-if="user.power == 1">
			<fui-input label="角色:" readonly value="管理员" borderTop></fui-input>
		</view>
		<view v-if="user.power == 2">
			<fui-input label="角色:" readonly value="监管人员" borderTop></fui-input>
		</view>
		<view v-if="user.power == 3">
			<fui-input label="角色:" readonly value="操作员" borderTop></fui-input>
		</view>
		<view>
			<fui-input label="账号:" readonly :value="user.nickName" borderTop></fui-input>
		</view>
		<!-- <view>
			<fui-input label="密码" readonly borderTop>
				<fui-button type="gray" @click="showPwd = !showPwd" bold width="200rpx" height="64rpx" :size="28"
					text="修改密码"></fui-button>
			</fui-input>
		</view> -->
		<view class="display_column">
			<view @click="onUserDel" class="com_width display_flex padding_tb_l color_all" style="border: 1px solid #eeeeee;border-radius: 13rpx;justify-content: center;">
				退出登录
			</view>
		</view>
	</view>
	<view>
		<u-modal :show="showPwd" title="修改密码" showCancelButton @confirm="onSetPwd" @cancel="showPwd =!showPwd">
			<view class="slot-content">
				<fui-input label="旧密码" v-model="olPwd" password borderTop placeholder="请输入旧密码"></fui-input>
				<fui-input label="新密码" v-model="newPwd" password borderTop placeholder="请输入新密码"></fui-input>
			</view>
		</u-modal>
	</view>
</template>

<script>
	import {
		BASE_IP
	} from '@/common/http/url.js'
	export default {
		name: "security",
		data() {
			return {
				user: "",
				showPwd: false,
				olPwd: "",
				newPwd: ""
			}
		},
		methods: {
			onUserDel() {
				uni.navigateTo({
					url:"/pages/login/login"
				})
				return;
				let user = uni.getStorageSync("user")
				if(user.power == 1){
					uni.showModal({
						title:"提示",
						content:"此用户不允许注销",
						showCancel:false
					})
					return
				}
				uni.showModal({
					title: "提示",
					content: "您确定要注销此账号吗？",
					success: (res) => {
						if (res.confirm) {
							
							let url = BASE_IP + "pc/user/remove/user/" + user.uuid
							// console.log(url)
							uni.request({
								header: {
									"Authorization": user.token
								},
								method: "DELETE",
								url: url,
								success: (res) => {
									if (res.statusCode == 200) {
										uni.showToast({
											title: "注销成功",
											icon: "success"
										})
										uni.clearStorageSync()
										uni.clearStorage()
										uni.redirectTo({
											url: "/pages/login/login"
										})
										

									}
									if (res.statusCode == 404) {
										uni.showToast({
											title: "注销失败",
											icon: "error"
										})
									}
									if (res.statusCode == 500) {
										uni.showToast({
											title: "注销失败",
											icon: "error"
										})
									}
								}
							})
						}
					}
				})
			},
			onSetPwd() {
				if (this.olPwd == "") {
					uni.showToast({
						title: "请输入旧密码",
						icon: "error"
					})
					return
				}
				if (this.newPwd == "") {
					uni.showToast({
						title: "请输入新密码",
						icon: "error"
					})
					return
				}
				if (this.newPwd.length < 8) {
					uni.showToast({
						title: "新密码长度小于8",
						icon: "error"
					})
					return
				}
				let url = BASE_IP + "pc/user/update/passWord/" + this.user.userName + "/" + this.olPwd + "/" + this.newPwd
				uni.request({
					header: {
						"Authorization": this.user.token
					},
					url: url,
					method: "POST",
					success: (res) => {
						if (res.statusCode == 500) {
							uni.showToast({
								title: "旧密码错误",
								icon: "error"
							})
							return
						}
						if (res.statusCode == 200) {
							uni.showModal({
								title: "提示",
								content: "修改成功，请重新登录",
								success: (res) => {
									if (res.confirm) {
										uni.removeStorageSync("user")
										uni.redirectTo({
											url: "/pages/login/login"
										})
									}
								}
							})
						}
					}
				})
			}
		},
		onLoad() {
			let user = uni.getStorageSync("user")
			this.user = user
			// console.log("获取用户对象", user)
		}
	}
</script>

<style>
	.securitys {
		width: 98%;
		height: 100%;
		margin: auto;
	}
</style>